<?php

namespace Authentication;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Mvc\ModuleRouteListener;

use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;

use Authentication\Model\RoleService;
use Roles\Model\Permiso;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

class Module implements AutoloaderProviderInterface
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    // if we're in a namespace deeper than one level we need to fix the \ in the path
                    __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
                ),
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig()
    {
        return array(

        // setting db config immediately if necessary, ignore if already defined in global.php
        // 'db' => array(
        // 'username' => 'YOUR USERNAME HERE',
        // 'password' => 'YOUR PASSWORD HERE',
        // 'driver' => 'Pdo',
        // 'dsn' => 'mysql:dbname=zf2tutorial;host=localhost',
        // 'driver_options' => array(
        // PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        // ),
        // ),

            'factories'=>array(
                // 'Zend\Db\Adapter\Adapter'
                    // => 'Zend\Db\Adapter\AdapterServiceFactory',

                'Authentication\Model\AuthStorage' => function($sm){
                    return new \Authentication\Model\AuthStorage('asistencia_login');
                },

                'AuthService' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'usuarios','username','password', 'MD5(?)');

                    $authService = new AuthenticationService();
                    $authService->setAdapter($dbTableAuthAdapter);
                    $authService->setStorage($sm->get('Authentication\Model\AuthStorage'));

                    return $authService;
                },

                /*'PermisoTableGateway' => function($sm) {
                   $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                   $resultSetPrototype = new ResultSet();
                   $resultSetPrototype->setArrayObjectPrototype(new Permiso());
                   return new TableGateway('permisos', $dbAdapter, null, $resultSetPrototype);
                },*/

                'RoleService' => function($sm) {
                   //$tableGateway = $sm->get('PermisoTableGateway');
                   //$table = new RoleService($tableGateway);
                   $table = new RoleService();
                   return $table;
                },

            ),
        );
    }

}
